import type { ProColumns } from '@ant-design/pro-components';
import { ProForm, ProFormDateRangePicker, ProTable } from '@ant-design/pro-components';
import type { Record, RecordParams } from '@fill/client';
import { rspc } from '@fill/client';
import { join } from '@tauri-apps/api/path';
import { Form } from 'antd';
import dayjs from 'dayjs';
import React, { useState } from 'react';
import { APPROVAL_REPORT_ENUM, MEETING_APPROVAL_ENUM, NODE_TYPE } from '../../constants/default';
import type { w_sp_history } from '../../lib/types/sp_history';
import type { IUploadFile } from '../../util';
import { filesPath, openFile } from '../../util';

export interface IRecordsProps {
  relation_uuid?: string | null
  module: NODE_TYPE
  start?: string | null
  end?: string | null
}

export const Records: React.FC<IRecordsProps> = ({
  relation_uuid = null,
  module,
  start = null,
  end = null,
}) => {
  const [params, setParams] = useState<RecordParams>({
    relation_uuid,
    module,
    start,
    end,
  });
  const { data, isLoading } = rspc.useQuery(['records.get', {
    arg: params,
  }]);
  const columns: ProColumns[] = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'indexBorder',
      width: 48,
    },
    {
      title: '发送时间',
      dataIndex: 'created_at',
      render(_, entity) {
        const item = JSON.parse(entity.content) as w_sp_history;
        return item.F_FSSJ_ || '-';
      },
    },
    {
      title: '审核单位',
      render(_, entity) {
        const item = JSON.parse(entity.content) as w_sp_history;
        return item.F_SHDWMC_ || '-';
      },
    },
    {
      title: '操作人',
      render(_, entity) {
        const item = JSON.parse(entity.content) as w_sp_history;
        return item.F_SHRMC_ || '-';
      },
    },
    {
      title: '审批意见',
      render(_, entity) {
        const item = JSON.parse(entity.content) as w_sp_history;
        const enums = module !== NODE_TYPE.BOOK ? MEETING_APPROVAL_ENUM : APPROVAL_REPORT_ENUM;
        return enums[+item.F_SPYJ_ as keyof typeof enums] || '-';
      },
    },
    {
      title: '审批说明',
      render(_, entity) {
        const item = JSON.parse(entity.content) as w_sp_history;
        return item.F_SPSM_ || '-';
      },
    },
    {
      title: '说明材料',
      render(_, entity) {
        const item = JSON.parse(entity.content) as w_sp_history;
        const attachments = JSON.parse(item.F_SMCL_ || '[]') as IUploadFile[];
        console.log(attachments);
        return attachments.map((_item, index) => {
          return <a key={index} onClick={async () => await openFile(await join(await filesPath(), _item.fileName as string))}>{_item.name}</a>;
        }) || '-';
      },
    },
    {
      title: '审批时间',
      render(_, entity) {
        return entity.approval_at || '-';
      },
    },
  ];

  const [form] = Form.useForm<any>();

  return (
    <ProTable<Record>
      columns={columns}
      rowKey="uuid"
      search={false}
      options={false}
      dataSource={data}
      loading={isLoading}
      headerTitle={
        <>
          <ProForm<{
            date: string
          }>
            layout="inline"
            form={form}
            submitter={{
              searchConfig: {
                submitText: '查询',
              },
              render(props, dom) {
                return dom.reverse();
              },
              onReset() {
                form.resetFields();
                form.submit();
              },
            }}
            onFinish={async (values) => {
              console.log(values);
              const [start, end] = values?.date || [null, null];
              console.log(dayjs('2023-03-07 17:08:47').valueOf().toString());
              setParams({
                relation_uuid,
                module,
                start: (start && `${start} 00:00:00`) || null,
                end: (end && `${end} 23:59:59`) || null,
              });
            }}
          >
            <ProFormDateRangePicker label="审批时间" name="date" />
          </ProForm>
        </>
      }
    />
  );
};
